<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Videocomments extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\VideoComments();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function lists()
    {
        $pageSize = input('pagesize', 15);

        $videoId = input('video_id');
        if (!$videoId) {
            $this->error('参数缺失！');
        }
        $where = [
            'status' => 1,
            'video_id' => $videoId,
            'comment_id1' => 0,
        ];

        $list = self::$model->where($where)->order('createtime desc')->limit($start, $pageSize)->select();
        if ($list) {
            foreach ($list as $val) {
                $where['comment_id1'] = $val->id;
                $val->user;
                $lists = self::$model->where($where)->order('createtime asc')->select();
                $val->lists = $lists;
                if ($lists) {
                    foreach ($lists as $vv) {
                        $vv->user;
                    }
                }
            }
        }
        $total = self::$model->where($where)->count();
        $this->success('视频评论列表', ['list' => $list, 'total' => $total]);
    }

    /**
     * 视频评论
     */
    public function create()
    {
        $videoId = input('video_id');
        if (!$videoId) {
            $this->error('参数缺失！');
        }

        $content = input('content');
        if (!$content) {
            $this->error('请输入评论内容！');
        }

        $userId = $this->auth->id;
        $cdata = [
            'user_id' => $userId,
            'content' => $content,
            'video_id' => $videoId,
        ];

        $commentId = input('comment_id');
        if ($commentId) {
            $row_info = \app\common\model\VideoComments::find($commentId);
            if (!$row_info) {
                $this->error('评论id不存在！');
            }
            if ($row_info->user_id == $userId) {
                $this->error('不可对自己的评论再次评论！');
            }

            if ($row_info->comment_id1 <= 0) {
                $cdata['comment_id1'] = $commentId;
            } else {
                $row_info_main = \app\common\model\VideoComments::find($row_info->comment_id1);
                if ($row_info_main->user_id != $userId) {
                    if ($row_info->touser_id != $userId) {
                        $this->error('您无权进行评论！');
                    }
                }
                $cdata['comment_id1'] = $row_info_main->id;
                $cdata['comment_id2'] = $row_info->id;
            }

            $cdata['touser_id'] = $row_info->user_id;
        }

        $ret = \app\common\model\VideoComments::create($cdata);
        if (!$ret) {
            $this->error('评论失败！');
        }

        $this->success('评论成功！');
    }
}
